package com.cnzhy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cnzhy.entity.CrmAdminUser;
import com.cnzhy.services.interfaces.CrmAdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zhaoyan5
 */
@RestController
public class CrmAdminUserController {

    /**
     * 不推荐使用字段注入
     */
    private CrmAdminUserService crmAdminUserService;

    /**
     * 推荐使用set方法注入
     */
    @Autowired
    public void setCrmAdminUserService(CrmAdminUserService crmAdminUserService) {
        this.crmAdminUserService = crmAdminUserService;
    }

    @GetMapping("user/{userId}")
    @SentinelResource("getCrmAdminUser")
    public CrmAdminUser getCrmAdminUser(@PathVariable long userId) {
        return crmAdminUserService.selectByPrimaryKey(userId);
    }

    @GetMapping("update/{userId}/{userName}")
    @Transactional(rollbackFor = Exception.class)
    public CrmAdminUser updateCrmAdminUser(@PathVariable long userId, @PathVariable String userName) {
        CrmAdminUser crmAdminUser = crmAdminUserService.selectByPrimaryKey(userId);
        crmAdminUser.setRealName(userName);
        crmAdminUserService.updateByPrimaryKey(crmAdminUser);
        return crmAdminUser;
    }
}
